#!/usr/bin/perl

use strict;
use POSIX; 

sub usage {
    print "usage: clear_log.pl <logfile> <count_days> \n";
}

if (!defined ($ARGV[1])) {
    &usage();
    exit 1;
}

my $logfile = $ARGV[0];
my $count_days = $ARGV[1]; 

open FILEIN, "<$logfile";
open FILEOUT, ">$logfile.new";

#current date
my $mday;
my $mon;
my $year;
($mday, $mon, $year) = (localtime)[3,4,5];

while (<FILEIN>) {
    if (/^[^ ]*[ ](.{4})\.(.{2})\.(.{2})/) {
	my $yyyy = $1;
	my $mm = $2;
	my $dd = $3;
	my $diff = difftime( mktime(0,0,0,$mday,$mon,$year), mktime(0,0,0,$dd,$mm-1,$yyyy-1900) ) / 86400;
	if ( $diff >= $count_days ) {next};
    }
    print FILEOUT $_;
}
close FILEIN;
close FILEOUT;

rename "$logfile.new", "$logfile";
chmod 0666, "$logfile";
